package src.serverSide;

import java.util.*;

import src.categoryStructures.*;



public class ItemCategories {
	
	private Map<CategoryLostFound,Map<CategoryType,Map<CategoryCity,TreeSet<Integer>>>> categorymap;
	
	public ItemCategories() {
		categorymap = new HashMap<CategoryLostFound,Map<CategoryType,Map<CategoryCity,TreeSet<Integer>>>>();
		Arrays.asList(CategoryLostFound.values());
		for (Map<CategoryType,Map<CategoryCity,TreeSet<Integer>>> i : categorymap.values()) {
			i = new HashMap<CategoryType,Map<CategoryCity,TreeSet<Integer>>>();
			for (Map<CategoryCity,TreeSet<Integer>> j : i.values()) {
				j = new HashMap<CategoryCity,TreeSet<Integer>>();
				for (TreeSet<Integer> k : j.values()) {
					k = new TreeSet<Integer>();
				}
			}
		}
	}
	
	public void addItem(int itemid, Categories categories) {
		for (CategoryLostFound i : categories.lostfound) 
			for (CategoryType j : categories.type) 
				for (CategoryCity k : categories.city) 
					categorymap.get(i).get(j).get(k).add(itemid);
	}
	
	/**
	 * This method removes all references to the given item ID
	 * @param itemid The item ID to delete
	 */
	public void removeItem(int itemid) {
		for (Map<CategoryType,Map<CategoryCity,TreeSet<Integer>>> i : categorymap.values()) 
			for (Map<CategoryCity,TreeSet<Integer>> j : i.values()) 
				for (TreeSet<Integer> k : j.values()) 
					categorymap.get(i).get(j).get(k).remove(itemid);
	}

	public List<Integer> getItemCategory(Category category, int start, int length) {
		TreeSet<Integer> set = categorymap.get(category.lostfound).get(category.type).get(category.city);
		Iterator<Integer> i = set.descendingIterator();
		List<Integer> list = new ArrayList<Integer>();
		while (i.hasNext() && i.next() > start);
		for (int j = 0; j < length && i.hasNext(); j++) list.add(i.next());
		return list;
	}
	
}
